package com.loadmore.common.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.loadmore.common.R;
import com.loadmore.common.util.ScreenUtil;

/**
 * @author sunleilei
 * @date 2017/10/15
 * @time 下午5:47
 */

public class ToolbarLayout extends android.support.v7.widget.Toolbar {
    LinearLayout left;
    LinearLayout center;
    LinearLayout right;
    ImageView backView;
    TextView titleView;
    private MenuClickListener menuClickListener;
    public ToolbarLayout(Context context) {
        this(context, null);
    }

    public ToolbarLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ToolbarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = LayoutInflater.from(context).inflate(R.layout.toolbar, null);
        addView(view);
        left = view.findViewById(R.id.left);
        center = view.findViewById(R.id.center);
        right = view.findViewById(R.id.right);
        backView = view.findViewById(R.id.back_view);
        titleView = view.findViewById(R.id.title);
    }

    public void setTitle(String title) {
        titleView.setText(title);
    }

    public void setTitle(int title) {
        titleView.setText(title);
    }

    public void showBackView() {
        backView.setVisibility(VISIBLE);
    }

    public void hideBackView() {
        backView.setVisibility(GONE);
    }

    public void setMenuClickListener(MenuClickListener menuClickListener) {
        this.menuClickListener = menuClickListener;
    }

    public TextView addLeftText(int id) {
        TextView textView = getText(id);
        left.addView(textView);
        setLayoutparam(textView);
        return textView;
    }

    public TextView addRightText(int id) {
        TextView textView = getText(id);
        right.addView(textView);
        setLayoutparam(textView);
        return textView;
    }

    public TextView addCenterText(int id) {
        TextView textView = getText(id);
        center.addView(textView);
        setLayoutparam(textView);
        return textView;
    }

    public ImageView addLeftImage(int id) {
        ImageView imageView = getImage(id);
        left.addView(imageView);
        setLayoutparam(imageView);
        return imageView;
    }

    public ImageView addRightImage(int id) {
        ImageView imageView = getImage(id);
        right.addView(imageView);
        setLayoutparam(imageView);
        return imageView;
    }

    public ImageView addCenterImage(int id) {
        ImageView imageView = getImage(id);
        center.addView(imageView);
        setLayoutparam(imageView);
        return imageView;
    }

    public void hideTitle() {
        titleView.setVisibility(GONE);
    }

    public void showTitle() {
        titleView.setVisibility(VISIBLE);
    }


    public TextView getText(final int id) {
        TextView textView = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.toolbar_text, null);
        textView.setId(id);
        textView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (menuClickListener != null) {
                    menuClickListener.onMenuClick(id);
                }
            }
        });
        return textView;
    }

    public ImageView getImage(final int id) {
        ImageView imageView = (ImageView) LayoutInflater.from(getContext()).inflate(R.layout.toolbar_image, null);
        imageView.setId(id);
        imageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (menuClickListener != null) {
                    menuClickListener.onMenuClick(id);
                }
            }
        });
        return imageView;
    }

    public void setLayoutparam(View view) {
        MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams();
        params.rightMargin = ScreenUtil.dp2px(10);
    }

    public interface MenuClickListener {
        public void onMenuClick(int id);
    }
}
